﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace Moneymatters
{
    public partial class DateTimePickerForm : Form
    {
        //[DllImport("user32.dll")]
        //static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        //private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        //private const int MOUSEEVENTF_LEFTUP = 0x0004;

        //static extern int SendMessage(IntPtr hWnd, uint uMsg,
        //                          int wParam, int lParam);
        
        //private const int WM_LBUTTONDOWN = 0x0201;
        //private const int WM_LBUTTONUP = 0x0202;

        public DateTime SelectedDateTime
        {
            get;
            set;
        }


        public DateTimePickerForm(DateTime? transactionDate)
        {
            InitializeComponent();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            if (transactionDate.HasValue)
            {
                this.dateTimePicker.Value = transactionDate.Value;
            }


            //this.dateTimePicker.MouseDown += dateTimePicker_MouseDown;
            //this.dateTimePicker.Show();
            //Point point = this.dateTimePicker.PointToScreen(new Point(this.dateTimePicker.Location.X,
            //                                            this.dateTimePicker.Location.Y));
            //Point parent = this.PointToScreen(new Point(this.Location.X, this.Location.Y));

            //int X = parent.X + 195;
            //int Y = point.Y + 22;

            //LeftClick(X, Y);
            
            
            
        }

        void dateTimePicker_MouseDown(object sender, MouseEventArgs e)
        {
            //ShowCalendar(e);
        }

        void dateTimePicker_DropDown(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
           
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.SelectedDateTime = this.dateTimePicker.Value;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        //public void ShowCalendar(MouseEventArgs clickEvent)
        //{
        //    if (this.dateTimePicker != null)
        //    {
        //        // Remove any existing event to prevent an infinite loop.
        //        //var suppressor = new EventSuppressor(picker);
        //        //suppressor.Suppress();

        //        // Get the position on the button.
        //        int x = this.dateTimePicker.Width - 10;
        //        int y = this.dateTimePicker.Height / 2;
        //        int lParam = x + y * 0x00010000;

        //        // Ignore if the calendar button was clicked
        //        if (clickEvent.X < this.dateTimePicker.Width - 35)
        //        {
        //            SendMessage(this.dateTimePicker.Handle, WM_LBUTTONDOWN, 1, lParam);
        //            SendMessage(this.dateTimePicker.Handle, WM_LBUTTONUP, 1, lParam);
        //        }

        //        //suppressor.Resume();
        //    }
        //}


        //public static void LeftClick(int X, int Y)
        //{
        //    mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
        //    mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
        //} 
    }
}
